//
//  XiaoximenParty.m
//  Xiaoximen_iOS
//
//  Created by Wang Zhongyu on 12-7-19.
//  Copyright (c) 2012年 Peking University. All rights reserved.
//

#import "XiaoximenParty.h"

@interface XiaoximenParty ()

@end

@implementation XiaoximenParty
@synthesize ID;
@synthesize host;
@synthesize title;
@synthesize persons = _persons;
@synthesize places = _places;
@synthesize timeIntervals = _timeIntervals;

- (id)initWithHostName:(NSString *)hostName
			themeTitle:(NSString *)themeTitle
			   partyID:(NSUInteger)partyID
{
	self = [super init];
	if (self) {
        // Custom initialization
		host = hostName;
		title = themeTitle;
		ID = partyID;
	}
	return self;
}
- (NSMutableArray *)places
{
	if (_places == nil) {
		_places = [[NSMutableArray alloc] init];
	}
	return _places;
}
- (NSMutableArray *)persons
{
	if (_persons == nil) {
		_persons = [[NSMutableArray alloc] init];
	}
	return _persons;
}
- (NSMutableArray *)timeIntervals
{
	if (_timeIntervals == nil) {
		_timeIntervals = [[NSMutableArray alloc] init];
	}
	return _timeIntervals;
}

- (void)addPersonWithPhoneString:(NSString *)phoneString
						userName:(NSString *)userName
						personID:(NSUInteger)personID
{
	for (XiaoximenPerson *eachPerson in self.persons) {
		if ([eachPerson.phone isEqualToString:phoneString] && [eachPerson.name isEqualToString:userName]) return ;
		if (eachPerson.ID == personID) return ;
	}
	XiaoximenPerson *person1 = [[XiaoximenPerson alloc] initWithPhoneString:phoneString userName:userName personID:personID];
	person1.party = self;
	[self.persons addObject:person1];
}
- (void)addPlaceWithPlaceName:(NSString *)name
					  placeID:(NSUInteger)placeID
{
	for (XiaoximenVotePlace *eachPlace in self.places) {
		if ([eachPlace.placeName isEqualToString:name]) return ;
		if (eachPlace.ID == placeID) return ;
	}
	XiaoximenVotePlace *place1 = [[XiaoximenVotePlace alloc] initWithPlaceName:name placeID:placeID];
	place1.party = self;
	[self.places addObject:place1];
}

- (void)addTimeWithStartDate:(NSDate *)start
					 endDate:(NSDate *)end
					  timeID:(NSUInteger)timeID
{
	for (XiaoximenVoteTime *eachTime in self.timeIntervals) {
		if ([eachTime.startDate isEqual:start] && [eachTime.endDate isEqual:end]) return ;
		if (eachTime.ID == timeID) return ;
	}
	XiaoximenVoteTime *time1 = [[XiaoximenVoteTime alloc] initWithStartDate:start endDate:end timeID:timeID];
	time1.party = self;
	[self.timeIntervals addObject:time1];
}

+ (NSArray *)demoParties
{
	NSMutableArray *parties = [[NSMutableArray alloc] init];
	
	XiaoximenParty *party1 = [[XiaoximenParty alloc] initWithHostName:@"谢家晔" themeTitle:@"学习包粽子(Demo)" partyID:9999];
	[parties addObject:party1];
	
	[party1 addPlaceWithPlaceName:@"农园" placeID:9999];
	[party1 addPlaceWithPlaceName:@"康博斯" placeID:9989];
	[party1 addTimeWithStartDate:[NSDate date] endDate:[NSDate dateWithTimeIntervalSinceNow:60*60] timeID:9999];
	[party1 addTimeWithStartDate:[NSDate dateWithTimeIntervalSinceNow:60*60] endDate:[NSDate dateWithTimeIntervalSinceNow:2*60*60] timeID:9989];
	[party1 addTimeWithStartDate:[NSDate dateWithTimeIntervalSinceNow:2*60*60] endDate:[NSDate dateWithTimeIntervalSinceNow:3*60*60] timeID:9979];
	
	[party1 addPersonWithPhoneString:@"15201234567" userName:@"曹衫" personID:9999];
	
	[party1 addPersonWithPhoneString:@"13810325249" userName:@"张塔" personID:9998];
	XiaoximenPerson *person2 = [party1.persons lastObject];
	[person2 noForPlaceID:9999];	
	[person2 yesForPlaceID:9989];
	[person2 yesForTimeID:9999];
	[person2 yesForTimeID:9989];
	[person2 yesForTimeID:9979];
	
	[party1 addPersonWithPhoneString:@"15210624793" userName:@"王仲禹" personID:9988];
	XiaoximenPerson *person3 = [party1.persons lastObject];	
	[person3 yesForPlaceID:9999];
	[person3 yesForPlaceID:9989];
	[person3 yesForTimeID:9999];
	[person3 yesForTimeID:9989];
	[person3 noForTimeID:9979];
	
	return parties;
}

@end
